package com.swm.yzgrtk.utils;


import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 配置文件存储工具类
 */
public class MapTransFile {

    /**
     * map转出配置文件
     *
     * @param map
     */
    public static void exportMap(Map map, String filepath) {
        // 创建一个Properties对象
        Properties properties = new Properties();
        // 将Map的内容放入Properties
        properties.putAll(map);
        try (OutputStream outputStream = new FileOutputStream(filepath)) {
            properties.store(new OutputStreamWriter(outputStream, "UTF-8"), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成文件
     */
    public static void createFile(String str, String file) {
        try {
            FileWriter fw = new FileWriter(file);
            PrintWriter pw = new PrintWriter(fw);
            /*pw.println("start  %~dp0\\rommaker.exe");
            pw.println("exit");*/
            pw.println(str);
            pw.close();
            System.out.println("Batch file created successfully.");
        } catch (IOException e) {
            System.out.println("Error creating batch file: " + e.getMessage());
        }
    }


    /**
     * 配置文件转map
     */
    public static Map<String, String> importMap(String filepath) {
        Properties properties = new Properties();
        try (InputStream inputStream = new FileInputStream(filepath)) {
            properties.load(new InputStreamReader(inputStream, "UTF-8"));
            Map<String, String> map = new LinkedHashMap<String, String>((Map) properties);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改配置文件
     */

    public static void updatePro( Map<String, String> map,String filePath) {
        Properties properties = new Properties();
        try (InputStream inputStream = new FileInputStream(filePath)) {
            properties.load(new InputStreamReader(inputStream, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 修改属性值
        for (String key : map.keySet()) {
            properties.setProperty(key, map.get(key));
        }
        try (OutputStream outputStream = new FileOutputStream(filePath)) {
            properties.store(new OutputStreamWriter(outputStream, "UTF-8"), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前元素的信息
     */
   /* public static JSONObject getProperty(String eletype, String eleName) {
        //获取配置文件中元素信息
        Map<String, String> pageMap = MapTransFile.importMap(StaParas.folderPath + "//" + StaParas.currentPage + ".properties");
        //加载所有元素
        JSONObject json = JSONObject.parseObject(pageMap.get(eletype));
        JSONObject paramJson = JSONObject.parseObject(json.get(eleName).toString());
        return paramJson;
    }

    *//**
     * 写入当前元素的信息
     *//*
    public static void setProperty(JSONObject paramJson) {
        //获取配置文件中元素信息
        Map<String, String> pageMap = MapTransFile.importMap(StaParas.folderPath + "//" + StaParas.currentPage + ".properties");
        //加载所有元素
        JSONObject eleJson = JSONObject.parseObject(pageMap.get(StaParas.currentEleType));
        eleJson.put(StaParas.currentEleName, paramJson);
        pageMap.put(StaParas.currentEleType, String.valueOf(eleJson));
        MapTransFile.exportMap(pageMap, StaParas.folderPath + "//" + StaParas.currentPage + ".properties");
    }*/


}
